package com.example.yuejian11.activity;

import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.yuejian11.R;
import com.example.yuejian11.bean.RegisterEntity;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import butterknife.BindView;
import butterknife.OnClick;
import info.hoang8f.widget.FButton;

public class RegisterActivity extends BaseActivity {


    private static final String TAG = "RegisterActivity";
    @BindView(R.id.username)//绑定button 控件
    EditText username;
    @BindView(R.id.password1)
    EditText password1;
    @BindView(R.id.password2)
    EditText password2;
    @BindView(R.id.register)
    FButton register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_register);
        super.onCreate(savedInstanceState);
    }

    @OnClick(R.id.register)//给 button 设置一个点击事件
    void loginMe() {

        final String un = username.getText().toString();
        final String pwd1 = password1.getText().toString();
        final String pwd2 = password2.getText().toString();

        if (StringUtils.isEmpty(un)) {
            ToastUtils.showShort("用户名不能为空");
            return;
        }
        if (StringUtils.isEmpty(pwd1) || StringUtils.isEmpty(pwd2)) {
            ToastUtils.showShort("两个密码都得输入");
            return;
        }

        if (!pwd1.equals(pwd2)) {
            ToastUtils.showShort("两个密码不相同");
            return;
        }

//     http://localhost:8888/yuejian6/public/index.php/json/index/register?name=qaq123&password=11
        EasyHttp.get("yuejian6/public/index.php/json/index/register")
                .params("name", un)
                .params("password", pwd1)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        RegisterEntity registerEntity = null;

                        try {
                            registerEntity = JSON.parseObject(data, RegisterEntity.class);
                        } catch (Exception e) {
                            ToastUtils.showShort("获取数据失败");
                            return;
                        }
                        if (!"0".equals(registerEntity.getError())) {
                            ToastUtils.showShort("注册失败: " + registerEntity.getMsg());
                            return;
                        }

                        ToastUtils.showShort("注册成功");
                        ActivityUtils.finishActivity(RegisterActivity.class);
                    }

                    @Override
                    public void onError(ApiException error) {
                        ToastUtils.showShort("注册失败");
                        Log.d(TAG, "onFailed: " + error);
                    }
                });

    }
}
